import router from '@/router'
import nprogress from 'nprogress'
import 'nprogress/nprogress.css'
import store from './store'

// 前置守卫

const whiteList = ['/login','/404'] // 定义白名单
router.beforeEach(async(to, from, next) => {
  nprogress.start()
  // 存在token
  if (store.getters.token) {
    if (to.path === '/login') {
      // 跳转到主页
      next('/')//next（地址）里面不加地址就走后置守卫
      //  nprogress.done()
    } else {
      if (!store.getters.userId) 
        await store.dispatch('user/getUserInfo')
        next()//放过
    }
  } else {
    //没有token
    // 查看白名单
    if (whiteList.includes(to.path)) {
        next()
    } else {
      next('/login')
        // nprogress.done()
     }
    
  }
 
})
// 后置守卫
router.afterEach((to, from, next) => {  
  nprogress.done()
})